using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using App.Metrics;
using App.Metrics.AspNetCore;
using App.Metrics.Extensions.Configuration;
using App.Metrics.Formatters.InfluxDB;
using App.Metrics.Reporting.InfluxDB;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace StudyAppMetircs
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
                                .Build();

            var influxOptions = new MetricsReportingInfluxDbOptions();
            configuration.GetSection(nameof(MetricsReportingInfluxDbOptions)).Bind(influxOptions);
            influxOptions.MetricsOutputFormatter = new MetricsInfluxDbLineProtocolOutputFormatter();

            var metrics = AppMetrics.CreateDefaultBuilder()
                                    .Configuration.ReadFrom(configuration)
                                    .Report.ToInfluxDb(options=> {
                                        options.InfluxDb.BaseUri = new Uri("Http://127.0.0.1:8086");
                                        //options.InfluxDb.UserName = "Spf";
                                        //options.InfluxDb.Password = "123456789";
                                        options.InfluxDb.Database = "MySecondBucket";
                                        options.InfluxDb.CreateDataBaseIfNotExists = true;
                                        options.MetricsOutputFormatter = new MetricsInfluxDbLineProtocolOutputFormatter();
                                    })
                                    //.Report.ToInfluxDb2(options =>
                                    //{
                                    //    options.InfluxDb2.BaseUri = new Uri("Http://127.0.0.1:8086");
                                    //    options.InfluxDb2.Bucket = "MySecondBucket";
                                    //    options.InfluxDb2.Organization = "MySelf";
                                    //    options.InfluxDb2.Token = "L5kPYl3icYxhY8mzma00RgFgfucC2Rrzhg6GYT_sxYOYSdx9PTHIyt4pgJvqpC367_Z0O1pLrgOx5oGx9pbb0A==";
                                    //    options.MetricsOutputFormatter = new MetricsInfluxDbLineProtocolOutputFormatter();
                                    //})
                                    .Build();

            return Host.CreateDefaultBuilder(args)
                          .ConfigureMetrics(metrics)
                          .UseMetrics()
                        .ConfigureWebHostDefaults(webBuilder =>
                        {
                            webBuilder.UseStartup<Startup>();
                        });
        }
    }
}
